package main

import (
	"fmt"
	"github.com/EverythingMe/inbloom/go/inbloom"
)

func main() {
	// create a blank filter - expecting 20 members and an error rate of 1/100
	f, err := inbloom.NewFilter(20, 0.01)
	if err != nil {
		panic(err)
	}

	// the size of the filter
	fmt.Println(f.Len())

	// insert some values
	f.Add("foo")
	f.Add("bar")

	// test for existence of keys
	fmt.Println(f.Contains("foo"))
	fmt.Println(f.Contains("wat"))

	fmt.Println("marshaled data:", f.MarshalBase64())

	// Output:
	// 24
	// true
	// false
	// marshaled data: oU4AZAAAABQAAAAAAEIAABEAGAQAAgAgAAAwEAAJAAA=

	// a 20 cardinality 0.01 precision filter with "foo" and "bar" in it
	data := "oU4AZAAAABQAAAAAAEIAABEAGAQAAgAgAAAwEAAJAAA="

	// load it from base64
	f,err  = inbloom.UnmarshalBase64(data)
	if err != nil {
		panic(err)
	}

	// test it...
	fmt.Println(f.Contains("foo"))
	fmt.Println(f.Contains("wat"))
	fmt.Println(f.Len())

	// dump to pure binary
	fmt.Printf("%x\n", f.Marshal())
	// Output:
	// true
	// false
	// 24
	// a14e006400000014000000000042000011001804000200200000301000090000
}
